<?php

/**
 * @Author:      zwh
 * @DateTime:    2016-4-8 09:37:43
 * @Description: 盘点盈亏表
 */
class ProfitAndLoss extends Eloquent
{
    protected $table = "profit_and_loss";

    /**
     * @Author:      zwh
     * @DateTime:    2016-4-8 10:01:57
     * @Description: 盘平处理
     */
    public static function implement($pal,$user){
        return DB::transaction(function () use ($pal,$user) {
            foreach ($pal as $p) {
                if($p->variance_number>0){
                    $msi_arr=ProfitAndLoss::Profit($user->shop_id,$p->item_type,$p->item_id,$p->variance_number,array(),array());
                    foreach ($msi_arr as $_msi) {//每个批次的去 更新批次耗材 的剩余数量
                        $msi=MaintenanceStorageItem::find($_msi->id);
                        $msi->current_number=$msi->current_number+$_msi->user_number;
                        $msi->save();
                        //添加 该出库单的出库耗材 批次
                        $cor=new CloseOutRecord;
                        $cor->pal_id=$p->id;
                        $cor->msi_id=$_msi->id;
                        $cor->msi_id=$_msi->user_number;
                        $cor->save();

                        //记录 库存操作记录
                        $mwf=new MaintenanceWarehouseFlow;
                        $mwf->mw_id=$mw->id;
                        $mwf->type=1;//入库
                        $mwf->item_id=$_msi->item_id;
                        $mwf->number=$_msi->user_number;
                        $mwf->created_by=$user->id;
                        $mwf->save();
                    }
                    //更改该耗材的库存数量
                    $mw->number=$mw->number+$p->variance_number;
                    $mw->save();
                }else{
                    $msi_arr=ProfitAndLoss::Loss($user->shop_id,$p->item_type,$p->item_id,abs($p->variance_number),array(),array());
                    foreach ($msi_arr as $_msi) {//每个批次的去 更新批次耗材 的剩余数量
                        $msi=MaintenanceStorageItem::find($_msi->id);
                        $msi->current_number=$msi->current_number-$_msi->user_number;
                        $msi->save();
                         //添加 该出库单的出库耗材 批次
                        $cor=new CloseOutRecord;
                        $cor->pal_id=$p->id;
                        $cor->msi_id=$_msi->id;
                        $cor->msi_id=$_msi->user_number;
                        $cor->save();
                        //记录 库存操作记录
                        $mwf=new MaintenanceWarehouseFlow;
                        $mwf->mw_id=$mw->id;
                        $mwf->type=1;//入库
                        $mwf->item_id=$_msi->item_id;
                        $mwf->number=$_msi->user_number;
                        $mwf->created_by=$user->id;
                        $mwf->save();
                    }
                    //更改该耗材的库存数量
                    $mw->number=$mw->number-abs($p->variance_number);
                    $mw->save();
                }
            }
            return array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$id));
        });
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-4-8 09:38:41
     * @Description: 根据耗材盘盈盘亏进行盘平处理
     *
     */
    public static function Loss($shop_id,$item_type,$item_id,$number,$finally_arr,$use_id){
        $msi=MaintenanceStorageItem::where("shop_id",$shop_id)
        ->where("status",1)->where("type",1)
        ->where("item_type",$item_type)
        ->where("item_id",$item_id)->where("current_number",">",0);
        if(sizeof($use_id)>0){
            $msi=$msi->whereNotIn("id",$use_id);
        }
        $msi=$msi->orderBy("created_at","desc")->first();
        if($msi->current_number>=$number){//说明这次递归找到的耗材批次已经能满足 出库需要
            $msi->user_number=$number;
            $finally_arr[]=$msi;
            return $finally_arr;
        }else{//说明该次 的 递归查找批次还是没满足 出库需要 递归下个批次
            $msi->user_number=$msi->current_number;
            $finally_arr[]=$msi;
            $use_id[]=$msi->id;
            return MaintenanceStorageItem::getUseStorageItem($shop_id,$item_type,$item_id,$number-$msi->current_number,$finally_arr,$use_id);
        }
    }


    /**
     * @Author:      zwh
     * @DateTime:    2016-4-8 09:38:41
     * @Description: 根据耗材盘盈盘亏进行盘平处理
     *
     */
    public static function Profit($shop_id,$item_type,$item_id,$number,$finally_arr,$use_id){
        $msi=MaintenanceStorageItem::where("shop_id",$shop_id)
        ->where("status",1)->where("type",1)
        ->where("item_type",$item_type)
        ->where("item_id",$item_id)->where("current_number",">",0);
        if(sizeof($use_id)>0){
            $msi=$msi->whereNotIn("id",$use_id);
        }
        $msi=$msi->orderBy("created_at","desc")->first();
        if($msi->number-$msi->current_number>=$number){//说明这次递归找到的耗材批次已经能满足 出库需要
            $msi->user_number=$number;
            $finally_arr[]=$msi;
            return $finally_arr;
        }else{//说明该次 的 递归查找批次还是没满足 出库需要 递归下个批次
            $msi->user_number=$msi->number-$msi->current_number;
            $finally_arr[]=$msi;
            $use_id[]=$msi->id;
            return MaintenanceStorageItem::getUseStorageItem($shop_id,$item_type,$item_id,$number-($msi->number-$msi->current_number),$finally_arr,$use_id);
        }
    }
}